package cs236369.hw5;

import java.util.Collection;
import java.util.LinkedList;

import org.apache.log4j.Logger;

import cs236369.hw5.db.DbHandler;
import cs236369.hw5.db.DbHandlerImp;
import cs236369.hw5.db.beans.DbMessage;

public class SearchWS
{
	private DbHandler db = new DbHandlerImp();
	
	public String[] searchKeywords(String[] keywords)
	{
		Logger.getLogger(getClass()).info("search keywords is not implemented...");
		return new String[]{"not implemented..."};
	}

	public String[] searchLocal(double locationX, double locationY, double d)
	{
		Logger l = Logger.getLogger(getClass());
		l.info("searchLocal axis web service generating new point:" + locationX + "," + locationY);

		Collection<DbMessage> msgs = db.find(locationX, locationY, d);
		l.info("[OK] found " + msgs.size() + " records.");
		Collection<String> res = new LinkedList<String>();
		for (DbMessage m : msgs)
		{
			
			StringBuilder builder = new StringBuilder();
			builder.append(m.getX());
			builder.append("||");
			builder.append(m.getY());
			builder.append("||");
			builder.append(m.getContent());
			String s = builder.toString();
			l.info("appending " + s + " to results.");
			res.add(s);
		}
		String [] result = res.toArray(new String[res.size()]);
		l.info("result size: " + result.length);
		if (result.length == 0)
		{
			result = new String[]{"result was not found, sorry"};
		}
		return result;
	}
}
